package com.muli.muwai.bao;


import com.muli.muwai.util.ExpAndImpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/demo")
public class Controller {
    @Autowired
    AdressService adressService;

    @PostMapping("/demo1")
    public Object aa(@RequestParam(value="phone") String phone) {
        suiBianQiMing q1 = new suiBianQiMing();
        //String phone = "13900087652";
        String name = "测试项目部";
        List<TraceTable> traceTables = q1.customerTracking(phone);
        if (traceTables.size() == 0) {
            return "查无此证";
        }
        for (TraceTable traceTable : traceTables) {
           /* String lifeStatus = traceTable.getLifeStatus();
            if (lifeStatus.equals("ineffective") || lifeStatus.equals("invalid")) {
                return "此合同未生效或作废";
            }*/
            //获取省
            String field18cemC = traceTable.getField18cemC();
            String sehngfen = adressService.sehngfen(field18cemC);
            traceTable.setField18cemC(sehngfen);

            //获取市
            String fieldfUV5cC = traceTable.getFieldfUV5cC();
            String shi = adressService.shi(fieldfUV5cC);
            traceTable.setFieldfUV5cC(shi);

            //获取区
            String qu = traceTable.getField7IT2aC();
            String qu1 = adressService.qu(qu);
            traceTable.setField7IT2aC(qu1);

            //获取服务人员
            String ownerr = traceTable.getHead();
            if (ownerr == null) {
                traceTable.setHead("");
            } else {
                String strip = StringUtils.strip(ownerr.toString(), "[]");
                List<IntfPrincipal> owner = q1.owner(strip);
                for (IntfPrincipal strings : owner) {
                    traceTable.setHead(strings.getName());
                }
            }
            //获取获知渠道
            String oChannel = traceTable.getOChannel();
            switch (oChannel) {
                case "fIxB527g9":
                    traceTable.setOChannel("设计师");
                    break;
                case "u8h7tuvHY":
                    traceTable.setOChannel("楼盘社群");
                    break;
                case "lch8z5133":
                    traceTable.setOChannel("异业合作");
                    break;
                case "QeFhqxwCu":
                    traceTable.setOChannel("老客户营销");
                    break;
                case "Wawdry1zI":
                    traceTable.setOChannel("网络媒介");
                    break;
                case "e09w8r1n6":
                    traceTable.setOChannel("自然客流");
                    break;
                case "other":
                    traceTable.setOChannel("其他");
                    break;
            }

        }
        List<TraceTable> resultList = traceTables.stream().filter(trace -> (Objects.equals(trace.getLifeStatus(), "normal")
                ||Objects.equals(trace.getLifeStatus(), "under_review")
                ||Objects.equals(trace.getLifeStatus(), "in_change")
        )&& Objects.equals(trace.getOwnerDepartment(), name)).collect(Collectors.toList());
        if (resultList.size() == 0) {
            return "此合同未生效或作废";
        }
        Collections.sort(resultList,new Comparator<TraceTable>(){
            @Override
            public int compare(TraceTable o1, TraceTable o2) {
                Timestamp dt1 = o1.getCreationTime();
                Timestamp dt2 = o2.getCreationTime();
                if (dt1.getTime() > dt2.getTime()) {
                    return -1;
                } else if (dt1.getTime() < dt2.getTime()) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });
        return resultList;
    }


}
